/*
* @Author: xiaobing00008
* @Date:   2018-10-03 16:50:39
* @Last Modified by:   xiaobing00008
* @Last Modified time: 2018-10-03 19:20:58
*/
import {CHANGE_INPUT_VALUE, ADD_TODO_ITEM, DELETE_TODO_ITEM} from './actionTypes'

//默认数据
const defaultState = {
    inputValue:'',
    list: []
}

//reducer可以接收state，但是绝不能修改state
const reducer = (state=defaultState,action) => {
    //判断用户传递过来的命令
    if(action.type === CHANGE_INPUT_VALUE) {
        //深拷贝原来数据 即store的老数据
        const newState = JSON.parse(JSON.stringify(state))
        //改变数据 根据用户传递过来的值
        newState.inputValue = action.value
        //返回新数据给store
        return newState
    }
    if(action.type === ADD_TODO_ITEM) {
        const newState = JSON.parse(JSON.stringify(state))
        //操作数据
        newState.list.push(newState.inputValue)
        newState.inputValue = ''
        return newState
    }

    if(action.type === DELETE_TODO_ITEM) {
        const newState = JSON.parse(JSON.stringify(state))
        //操作数据
        newState.list.splice(action.index,1)
        return newState
    }
    //第一次，没进判断语句时，返回state
    return state
}

export default reducer